package com.qys.livesMall.shop.event.aspect;

import com.qys.livesMall.finance.model.dto.AccountHandle;
import com.qys.livesMall.shop.enums.ShopEventTypeEnum;
import com.qys.livesMall.shop.event.ShopEvent;
import com.qys.livesMall.shop.event.shop.pojo.ShopIncomePojo;
import com.qys.livesMall.shop.event.shop.pojo.ShopRechargeWithdrawalPojo;
import com.qys.livesMall.shop.modal.entity.ShopEventInfo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.Objects;

import static com.qys.livesMall.finance.enums.AccountBusinessTypeEnum.*;


/**
 * @menu ShopEventAspect
 * @date 2023/11/07 9:26
 */
@Aspect
@Component
@Slf4j
public class ShopEventAspect {

    @Resource
    private ApplicationContext applicationContext;

    /**
     * 处理完请求后执行
     *
     * @param res 返回值
     */
    @AfterReturning(value = "execution(* com.qys.livesMall.finance.service.impl.FinanceShopAccountServiceImpl.directIn(..))", returning = "res")
    public void doAfterReturning(AccountHandle res) {
        if (!res.getSuccess()){
            return;
        }
        //如果是店铺收益
        if(Objects.equals(res.getBusinessType(),ORDER_PROFIT)
                ||Objects.equals(res.getBusinessType(),ORDER_PROFIT_SUPERIOR_GET)){
            applicationContext.publishEvent(new ShopEvent(ShopEventInfo.builder()
                    .shopId(res.getShopId())
                    .eventType(ShopEventTypeEnum.SHOP_INCOME.getType())
                    .paramObj(ShopIncomePojo.builder()
                            .amountIncurred(res.getAmountIncurred())
                            .build()
                    ).build())
            );
        }
    }

}

